package com.taru.houseapp.controller;

import com.taru.houseapp.commen.JsonResult;
import com.taru.houseapp.entity.HouseComment;
import com.taru.houseapp.service.IHComService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class HComcontroller {

    @Autowired
    IHComService comService;

    /**
     * 添加评论
     * @param comment
     * @return
     */
    @RequestMapping(value = "/house/comment",method = RequestMethod.POST)
    public JsonResult insertHouseComment(@RequestBody HouseComment comment){
        JsonResult result=null;
        String context=comment.getContext();
        String houseId=comment.getHouseId();
        String userId=comment.getUserId();
        if(comment==null || context==null || context.length()<=0 || houseId==null || houseId.length()<=0 || userId==null || userId.length()<=0){
            result =new JsonResult(404,"参数错误");
            return result;
        }
        try{
            comService.insertHouseComment(comment);
            result=new JsonResult(200,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 删除评论
     * @param ids
     * @return
     */
    @RequestMapping(value = "/house/comment",method = RequestMethod.DELETE)
    public JsonResult deleteHouseComment(@RequestBody List<Integer> ids){
        JsonResult result=null;
        if(ids==null || ids.size()<=0){
            result =new JsonResult(404,"参数错误");
            return result;
        }
        try{
            comService.deleteHouseComment(ids);
            result=new JsonResult(200,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }

    /**
     * 查看房子评论
     * @param houseId
     * @return
     */
    @RequestMapping(value = "/house/comment",method = RequestMethod.GET)
    public JsonResult queryHouseComment(String houseId){
        JsonResult result=null;
        if(houseId==null || houseId.length()<=0){
            result =new JsonResult(404,"参数错误");
            return result;
        }
        try{
            List<HouseComment> comments= comService.queryHouseComment(houseId);
            result=new JsonResult(200,"获取成功",comments);
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误",e.getMessage());
        }
        return result;
    }
}
